<?php

namespace wechat\models;

use Yii;
use yii\base\Behavior;
use yii\helpers\Json;

use EasyWeChat\Message\Text;
use EasyWeChat\Message\Image;
use EasyWeChat\Message\Voice;
use EasyWeChat\Message\News;
use EasyWeChat\Message\Article;

class DefaultBehavior extends Behavior {
    public function events() {
        return [
            Wechat::EVENT_AFTER_PROCESS => 'displayRequest',
        ];
    }
    
    public function displayRequest($event) {
        $owner = $this->owner;
        if (empty($owner->response)) {
            $owner = $this->owner;
            $message = $owner->message;
            $msgType = $message->get('MsgType');  
            $template = 'hi, {{nickname}}, {{FromUserName}}, you are welcome, msgType={{msgType}}';
            $content = strtr($template, [
                '{{nickname}}' => $owner->wxUser->nickname,
                '{{FromUserName}}' => $message->FromUserName,
                '{{msgType}}' => $message->MsgType,
            ]);
            $owner->response = new Text(['content' => $content]);                    
        }
    }
}
